import {getTopMovie} from '../../service/video-api';
Page({
    data: {
        topMovies: [],
        hasMore: true
    },
    stopNavLoading() {
        // 停止下拉效果
        wx.stopPullDownRefresh();
        // 取消头部加载动画
        wx.hideNavigationBarLoading();
    },
    // 请求数据
    async loadMovieData(offset = 0, limit) {
        // 头部加载动画
        wx.showNavigationBarLoading();
        if(!this.data.hasMore && offset !== 0) return this.stopNavLoading();

        const {data, hasMore} = await getTopMovie(offset, limit);

        this.setData({
            topMovies: offset === 0 ? data : this.data.topMovies.concat(data),
            hasMore
        })
        this.stopNavLoading();
    },
    // 页面加载
    onLoad() {
        this.loadMovieData(0, 20);
    },
    // 上拉触底
    onReachBottom() {
        this.loadMovieData(this.data.topMovies.length);
    },
    // 下拉刷新
    onPullDownRefresh(){
       this.loadMovieData(0);
    },
    movieItemClick(e) {
        const id = e.currentTarget.dataset.movieinfo.id;
        wx.navigateTo({
          url: '/pages/video-detail/index?id=' + id,
        })
    }
})